package com.example.administrator.smartdevice.base;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.smartdevice.activity.LoginActivity;
import com.example.administrator.smartdevice.net.RequestCallback;
import com.example.administrator.smartdevice.utils.AgentConstants;
import com.example.administrator.smartdevice.widget.CustomDialog;
import com.example.administrator.smartdevice.widget.CustomTipDialog;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/16.
 */

public abstract class BaseFragment extends Fragment implements RequestCallback {
    protected BaseActivity mBaseActivity;
    protected CustomTipDialog tipDialog;//加载对话框

    protected abstract void initData(View view, Bundle savedInstanceState);

    //获取布局文件 ID
    protected abstract int getLayoutId();

    //获取宿主Activity
    protected BaseActivity getmBaseActivity() {
        return mBaseActivity;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mBaseActivity = (BaseActivity) activity;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 弹出消息
     */
    protected void showToast(String msg) {
        Toast.makeText(mBaseActivity, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        ButterKnife.bind(this, view);
        initData(view, savedInstanceState);
        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    protected abstract void onFailure(int requestCode, Object object);

    protected abstract void onSuccess(int requestCode, Object object);

    @Override
    public void onSuccessCallback(int requestCode, Object object) {
        onSuccess(requestCode, object);
    }

    @Override
    public void onFailureCallback(int requestCode, Object object) {
        onFailure(requestCode, object);
    }

    /**
     * 页面跳转
     */
    protected void startOtherActivity(Class<?> className, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(mBaseActivity, className);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 弹出提示对话框
     * canceable: back键可以取消：true
     * 不可以取消：false
     */
    protected void startTipDialog(int iconType, String message, boolean cancelable) {
        if (tipDialog == null) {
            tipDialog = new CustomTipDialog(mBaseActivity);
            tipDialog.setIconType(iconType);
            tipDialog.setMessage(message);
            tipDialog.setCanceledOnTouchOutside(false);
            tipDialog.setCancelable(cancelable);
        }
        tipDialog.show();
    }

    /**
     * 销毁加载对话框
     */
    protected void stopTipDialog() {
        if (tipDialog != null) {
            tipDialog.dismiss();
        }
        tipDialog = null;
    }

    public void exit() {
        Intent intent = new Intent();
        intent.setClass(mBaseActivity, LoginActivity.class);
        intent.setAction(AgentConstants.Action.EXIT_APP);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }

    /**
     * 弹出对话框
     */
    protected void showInformationDialog(String message, int which1, int which2,
                                         DialogInterface.OnClickListener onClickListener) {
        new CustomDialog.Builder(mBaseActivity)
                .setTitle("智能家居")
                .setMessage(message)
                .setButton(which1, "确定", onClickListener)
                .setButton(which2, "取消", onClickListener)
                .setCancelable(false)
                .create()
                .show();
    }
}
